package com.miniapp.service.impl;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.ServiceException;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.SimplifiedObjectMeta;
import com.miniapp.service.RemoteFileService;
import org.springframework.stereotype.Service;

import java.io.InputStream;

/**
 * 远程文件服务类
 */
@Service
public class RemoteFileServiceImpl implements RemoteFileService {

    private static String endpoint = "http://oss-cn-beijing.aliyuncs.com";
    private String accessKeyId = "LTAIGnnyxn3gFgZs";
    private String accessKeySecret = "BHcqtxoWqU6lQuZhweyloA2lSfNH8F";
    private static String bucketName = "venice-test-bucket";


    @Override
    public SimplifiedObjectMeta uploadToAliyun(String saveRemoteFilePath, InputStream uploadFileInputStream) {

        OSSClient client = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            client.putObject(new PutObjectRequest(bucketName, saveRemoteFilePath, uploadFileInputStream));
            return client.getSimplifiedObjectMeta(bucketName, saveRemoteFilePath);
        } catch (Exception e) {
            throw new ServiceException("上传文件失败", e);
        }finally {
            client.shutdown();
        }
    }
}
